package com.luyun.frogpay.view.login;

import com.blankj.utilcode.util.ToastUtils;
import com.luyun.frogpay.base.baseclass.BaseObserver;
import com.luyun.frogpay.base.baseclass.BasePresenter;
import com.luyun.frogpay.base.baseclass.BaseResponse;
import com.luyun.frogpay.base.global.Constants;
import com.luyun.frogpay.base.global.SPKey;
import com.luyun.frogpay.base.utils.MD5Utils;
import com.luyun.frogpay.pojo.request.RequestLogin;
import com.luyun.frogpay.pojo.response.LoginRsp;
import com.orhanobut.hawk.Hawk;

import javax.inject.Inject;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginView> implements LoginContract.ILoginPresenter {

    @Inject
    LoginModel loginModel;

    @Inject
    public LoginPresenter() {}

    @Override
    public void login() {
        view.isShowLoading(true);
        String encryptedPassword = MD5Utils.MD5Upper(MD5Utils.MD5Lower(view.getPassword()) , Constants.LOGIN_SAFE_CODE);
        RequestLogin requestLogin = new RequestLogin("mch", "wxface_app", view.getAccount(), encryptedPassword);
        loginModel.login(requestLogin)
                .subscribe(new BaseObserver<LoginRsp>() {
                    @Override
                    protected void onSuccees(BaseResponse<LoginRsp> t) throws Exception {
                        Hawk.put(SPKey.LOGIN_RSP, t.getData());
                        view.isShowLoading(false);
                        view.loginSuccess();
                    }

                    @Override
                    protected void onCodeError(BaseResponse<LoginRsp> t) throws Exception {
                        view.isShowLoading(false);
                        ToastUtils.showShort(t.getMsg());
                    }

                    @Override
                    protected void onFailure(String errorMsg, boolean isNetWorkError) throws Exception {
                        view.isShowLoading(false);
                        ToastUtils.showShort(errorMsg);
                    }
                });
    }
}
